package com.vueperson.vueperson.t1.clone.serializableClone;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


import java.io.*;

/**
 * 利用序列化和反序列化进行对象的深拷贝
 */
 public class DeepClone implements Serializable {
    private static final long serialVersionUID = 1226623747237180757L;

    public Object deepClone() throws Exception{
        //序列化
        ByteArrayOutputStream bos=new ByteArrayOutputStream();
        ObjectOutputStream oos=new ObjectOutputStream(bos);
        oos.writeObject(this);
        //反序列化
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return ois.readObject();
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Person extends DeepClone{


    private static final long serialVersionUID = -1629699665468939625L;
    private int age;
    private String name;
    private Address address;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
class Address extends DeepClone{
    private static final long serialVersionUID = -434849143852532911L;
    private String province;
    private String street;
}

 class Client{
     public static void main(String[] args) throws Exception {
         Person person = new Person(15, "sowhat", new Address("河北", "建华南大街"));
         Person clonePerson = (Person) person.deepClone();
         System.out.println(person);
         System.out.println(clonePerson);

         clonePerson.setName("sowhat1412");
         clonePerson.setAge(20);
         Address address = clonePerson.getAddress();
         address.setStreet("中山路");

         System.out.println(person);
         System.out.println(clonePerson);

     }
}